/**
 * Classname :PagingInfo.java
 *
 * Version information: 1.0
 *
 * Date: 20140315
 *
 */
package com.astrocsr.domain.db;

import java.io.Serializable;

public class PagingInfo implements Serializable, Cloneable {
    /**
     * 
     */
    private static final long serialVersionUID = -2128624921508347105L;

    public final static int DEFAULT_INDEX = 1;
    public final static int DEFAULT_OFFSET = 50;
    public final static int DEFAULT_MAX_TOTAL_DISPLAY_PAGE = 50;
    
    private int offset = DEFAULT_OFFSET;
    private long total = 0;
    
    private int indexPage = DEFAULT_INDEX;

    /**
     * 
     */
    public PagingInfo() {
    }

    /**
     * 
     * @param offset
     */
    public PagingInfo(int offset) {
        this.setOffset(offset);
    }

    /**
     * 
     * @param offset
     * @param indexPage
     */
    public PagingInfo(int offset, int indexPage) {
        this.setOffset(offset);
        this.setIndexPage(indexPage);
    }

    /**
     * @return the index
     */
    public int getIndex() {
        return (indexPage - 1) * offset;
    }

    /**
     * @return the offset
     */
    public int getOffset() {
        return offset;
    }

    /**
     * @param offset
     *            the offset to set
     */
    public void setOffset(int offset) {
        if (offset > 0) {
            this.offset = offset;
        }
    }

    /**
     * @return the total
     */
    public long getTotal() {
        return total;
    }

    /**
     * @param total
     *            the total to set
     */
    public void setTotal(long total) {
        this.total = total;
    }

    /**
     * @return the indexPage
     */
    public int getIndexPage() {
        return indexPage;
    }

    /**
     * @param indexPage
     *            the indexPage to set
     */
    public void setIndexPage(int indexPage) {
        if (indexPage > 0) {
            this.indexPage = indexPage;
        }
    }

    /**
     * @return the totalPage
     */
    public long getTotalPage() {
        if (offset == 0) {
            return 0;
        } else {
            long totalPage = (total / offset);
            long divValue = (total % offset);
            return (divValue != 0 ? totalPage + 1 : totalPage);
        }
    }

    public String toString() {
        return "PagingInfo-[index:" + getIndex() + ", offset:" + offset + ", total:" + total + ", indexPage:" + indexPage + ", totalPage:" + getTotalPage() + "]";
    }
    
    public boolean isHasNextPage() {
    	return total > indexPage * offset;
    }
    
    public long getRemainingItems() {
    	if(!isHasNextPage()) {
    		return 0;
    	}
    	return total - indexPage * offset;
    }
}
